package Q1;

public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     *
     * @param str string字符串 原串
     * @param arg char字符型一维数组 需替换数组
     * @return string字符串
     */
    public String formatString (String str, char[] arg) {
        // write code here
        char[] ch = str.toCharArray();

        int argP = 0;
        int len = str.length();
        for (int i = 0; i < len; i++) {
            if (ch[i] == '%' && i < len - 1 && ch[i + 1] == 's') {
                ch[i++] = ' ';
                ch[i] = arg[argP++];
            }
        }

        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < len; i++) {
            if (ch[i] != ' ') {
                stringBuilder.append(ch[i]);
            }
        }

        while (argP < arg.length) {
            stringBuilder.append(arg[argP++]);
        }

        return stringBuilder.toString();
    }
}